﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ConfApp.Infrastructure;
using ConfApp.Models;
using ConfApp.Services;
using System.Windows.Navigation;
using Microsoft.Practices.Prism.Commands;

namespace ConfApp.ViewModels
{
    public class SessionDetailsViewModel : ViewModel
    {
        private readonly IConfDataStoreLocator _confDataStoreLocator;

        private ObservableCollection<SpeakerInfoViewModel> _observableSessionSpeakerInfos;
        private CollectionViewSource _sessionSpeakerInfosViewSource;

        public SessionDetailsViewModel(
            IConfDataStoreLocator confDataStoreLocator, 
            INavigationService navigationService) 
            : base(navigationService)
        {
            _confDataStoreLocator = confDataStoreLocator;
            Initialize();
        }

        private void Initialize()
        {
            this.SpeakerSelectedCommand = new DelegateCommand<SpeakerInfoViewModel>(
                s =>
                    {
                            NavigationService.Navigate(
                                new Uri("/Views/SpeakerDetailsView.xaml?ID=" + s.ID, UriKind.Relative));
                    });
            this.ToggleFavCommand = new DelegateCommand(
                () =>
                    {
                        IsFavourite = !IsFavourite;
                    });

            this.MapCommand = new DelegateCommand(
                () =>
                    {
                        var room = Session.Room.Code;
                        if (room.EndsWith("/1") || room.EndsWith("salon"))
                        {
                            NavigationService.Navigate(
                                new Uri("/Views/Map1View.xaml?ID=" + room, UriKind.Relative));
                        }
                        else
                        {
                            NavigationService.Navigate(
                                new Uri("/Views/Map0View.xaml?ID=" + room, UriKind.Relative));
                        }
                    });
            this.IsBeingActivated();
        }

        private int? _id;
        private Session _session;
        public Session Session
        {
            get
            {
                var id = int.Parse(NavigationService.CurrentSource
                                       .OriginalString.Split('=')[1]);
                if(_id == null || (_id != id))
                {
                    _id = id;
                    _session = _confDataStoreLocator.GetStore().FindSession(id);
                }
                return _session;
            }
        }

        public string Title { get { return Session.Title; } }
        public string Track { get { return Session.Track.Code; } }
        public string Slot { get { return Session.Slot.ToTimeSpanString(); } }

        public int Level { get { return _session.Level; } }
        public string Language { get { return _session.LanguageCode; } }
        public string Room { get { return _session.Room.Code; } }
        public string TrackCode { get { return _session.Track.Code; } }
        public string MinorInfo { get { return string.Format("{0} {1} {2} {3}", TrackCode, Level, Room, Language); } }
        public string FullInfo { get { return string.Format("{0}, {1} {2} {3} {4}", SlotInfo, TrackCode, Level, Room, Language); } }

        public Brush TrackColor
        {
            get
            {
                return new SolidColorBrush(
                HandleWhiteColor(_session.Track.Color));
            }
        }

        private Color HandleWhiteColor(Color color)
        {
            if (LightThemeEnabled)
            {
                if (color == Colors.White)
                {
                    return Colors.Black;
                }
            }
            else
            {
                if (color == Colors.Black)
                {
                    return Colors.White;
                }
            }
            return color;
        }

        public bool LightThemeEnabled
        {
            get
            {
                return (Visibility)Application.Current.Resources["PhoneLightThemeVisibility"] == Visibility.Visible;
            }
        }

        public string SlotInfo 
        {
            get { return string.Format("{0} {1}", _session.Slot.Day.Index == 0 ? "sre." : "čet.", _session.Slot.ToTimeSpanString()); }
        }

        public bool IsFavourite
        {
            get
            {
                return Session.IsFavourite; 
            }
            set
            {
                Session.IsFavourite = value;
                this.RaisePropertyChanged(() => this.AppBarFavIconUrl);
                this.RaisePropertyChanged(() => this.AppBarFavText);
                _confDataStoreLocator.GetStore().SaveFavouritesStore();
            }
        }

        public Uri AppBarFavIconUrl
        {
            get { return new Uri(IsFavourite ? 
                "/Resources/Icons/appbar.minus.rest.png" :
                "/Resources/Icons/appbar.favs.addto.rest.png",UriKind.Relative);
            }
        }

        public string AppBarFavText
        {
            get
            {
                return IsFavourite ?
                    "ukloni" :
                    "dodaj";
            }
        }
        public string Description
        {
            get
            {
                //return Session.Description;
                return Session.Description.Replace("\n",System.Environment.NewLine); 
            } }

        public ICollectionView SessionSpeakerInfos
        {
            get
            {
                _observableSessionSpeakerInfos = new ObservableCollection<SpeakerInfoViewModel>();
                Session.Speakers.Select(s=>new SpeakerInfoViewModel(s,NavigationService)).ToList()
                    .ForEach(_observableSessionSpeakerInfos.Add);

                _sessionSpeakerInfosViewSource = new CollectionViewSource { Source = _observableSessionSpeakerInfos };

                return _sessionSpeakerInfosViewSource.View;
            }
        }

        public DelegateCommand<SpeakerInfoViewModel> SpeakerSelectedCommand { get; set; }
        public DelegateCommand ToggleFavCommand { get; set; }
        public DelegateCommand MapCommand { get; set; }

        public override void IsBeingActivated()
        {
        }

    }
}
